package com.glodon.paas.document.dao.mysql;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.document.dao.ProjectRoleDao;
import com.glodon.paas.document.dao.domain.ProjectRoleEntity;
import com.glodon.paas.document.dao.mapper.ProjectRoleMapper;
import com.glodon.paas.util.DateUtil;

public class ProjectRoleDaoMysqlImpl implements ProjectRoleDao {

    @Autowired
    private ProjectRoleMapper projectRoleMapper;

    @Override
    public String insert(ProjectRoleEntity entity) {
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(DateUtil.getNow());
        }
        entity.setUpdateTime(entity.getCreateTime());
        projectRoleMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public ProjectRoleEntity get(String id) {
        return projectRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(String id) {
        return projectRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ProjectRoleEntity> getRoles(String projectId) {
        return projectRoleMapper.selectAllRoles(projectId);
    }

    @Override
    public boolean updateRole(ProjectRoleEntity entity) {
        return projectRoleMapper.updateByPrimaryKey(entity) > 0;
    }

    @Override
    public ProjectRoleEntity getDefaultRole(String projectId) {
        return projectRoleMapper.getDefaultRole(projectId);
    }

    @Override
    public boolean checkSameRole(ProjectRoleEntity role) {
        return projectRoleMapper.checkSameRole(role) > 0;
    }

    @Override
    public List<ProjectRoleEntity> getRolesByFile(String fileId) {
        return projectRoleMapper.selectAllRolesByFile(fileId);
    }
    
    public void setProjectRoleMapper(ProjectRoleMapper projectRoleMapper) {
        this.projectRoleMapper = projectRoleMapper;
    }
}
